﻿using BigEgg.Framework.Foundation;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace FMStudio.Domain.SQLite
{
    [MetadataType(typeof(IItemPrice))]
    public partial class ItemPrice : IItemPrice, IDataErrorInfo
    {
        #region Implement IDataErrorInfo Interface
        [NonSerialized]
        private readonly DataErrorInfoSupport dataErrorInfoSupport;

        string IDataErrorInfo.Error { get { return this.dataErrorInfoSupport.Error; } }

        string IDataErrorInfo.this[string memberName] { get { return this.dataErrorInfoSupport[memberName]; } }
        #endregion


        public ItemPrice()
        {
            this.dataErrorInfoSupport = new DataErrorInfoSupport(this);

            //  Default value
            Price = 1.0;

            this.ItemReference.AssociationChanged += ItemReferenceAssociationChanged;
            this.MarketReference.AssociationChanged += MarketReferenceAssociationChanged;
        }


        #region Implement IItemPrice Interface
        IMarket IItemPrice.Market
        {
            get { return this.Market; }
            set { this.Market = value as Market; }
        }

        IItem IItemPrice.Item
        {
            get { return this.Item; }
            set { this.Item = value as Item; }
        }
        #endregion


        #region Private Methods
        private void ItemReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("Item");
            this.ItemID = this.Item.ID;
        }

        private void MarketReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("Market");
            this.MarketID = this.Market.ID;
        }
        #endregion
    }
}
